﻿
// CountdownToRestartDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "CountdownToRestart.h"
#include "CountdownToRestartDlg.h"
#include "afxdialogex.h"
#include <iostream>
#include <string>

#include "Poco/Util/WinRegistryKey.h"

#include "../Utils/ComTools.h"
#include "../Utils/ResultError.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define REBOOT_TIMER 1
#define REBOOT_TIME  1 * 1000

#define REGIST_PATH "HKEY_CURRENT_USER\\SOFTWARE\\Yunny"

const std::string RegistryValue = "AutoReboot";

// CCountdownToRestartDlg 对话框

CCountdownToRestartDlg::CCountdownToRestartDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_COUNTDOWNTORESTART_DIALOG, pParent),
	m_yunnyRegistry(REGIST_PATH)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCountdownToRestartDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CCountdownToRestartDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CTLCOLOR()
	ON_WM_TIMER()
END_MESSAGE_MAP()


// CCountdownToRestartDlg 消息处理程序

void CCountdownToRestartDlg::WriteRegistry() {
	std::string ret = m_yunnyRegistry.setString(RegistryValue, "true");
	if (!ret.empty()) {
		// 设置值时不为空则出现异常
		std::wstring msg = yzq::GetLastErrorMsg(yzq::GetLastErrorCode());
		//MessageBox(msg.c_str());
		//MessageBox(std::to_wstring(yzq::GetLastErrorCode()).c_str());
		PostMessage(WM_QUIT);
	}
}

BOOL CCountdownToRestartDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// 查询重启状态
	std::string ret = m_yunnyRegistry.getString(RegistryValue);
	if (ret.empty()) {
		WriteRegistry();
	}
	else if (ret == "error"){
		//异常，需要判断是不是没找到值
		if (yzq::GetLastErrorCode() == 0) {
			// 没有此值，继续执行
			WriteRegistry();
		}
		else {
			std::wstring msg = yzq::GetLastErrorMsg(yzq::GetLastErrorCode());
			MessageBox(msg.c_str());
			MessageBox(std::to_wstring(yzq::GetLastErrorCode()).c_str());
			PostMessage(WM_QUIT);
		}
	}
	else if (ret == "true") {
		PostMessage(WM_QUIT);
	}

	//std::string key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Yunny";
	//m_winRegistry(new Poco::Util::WinRegistryKey(key, true));

	//QueryValue();

	int cx = GetSystemMetrics(SM_CXSCREEN);
	int cy = GetSystemMetrics(SM_CYSCREEN);

	// 设置字体模板
	m_font.CreatePointFont(150, L"微软雅黑");

	m_cTooltipText = new CStaticEx();
	std::wstring wsText = L"首次启动需要重启计算机使相关配置生效，即将为您重启计算机，请耐心等待，倒计时" + std::to_wstring(m_nClock);
	m_cTooltipText->Create(wsText.c_str(), WS_CHILD | WS_VISIBLE | SS_CENTER, CRect(50, 160, cx, 300), this);

	SetTimer(REBOOT_TIMER, REBOOT_TIME, NULL);

	//控件居中

	CRect rect;
	m_cTooltipText->GetWindowRect(&rect);
	m_cTooltipText->SetWindowPos(NULL, cx / 2 - rect.Width() / 2, cy / 2 - rect.Height() / 2, 0, 0, SWP_NOSIZE);

	//最大化，覆盖任务栏
	SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0);

	//置顶
	SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

	//删除注册表
	//WinExec("cmd /C \"echo Y | reg delete HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run \/v AutoReboot \/f\"", SW_HIDE);

	//窗体透明
	::SetWindowLong(GetSafeHwnd(), GWL_EXSTYLE, ::GetWindowLongPtr(GetSafeHwnd(), GWL_EXSTYLE) | WS_EX_LAYERED);
	this->SetLayeredWindowAttributes(0, (255 * 75) / 100, LWA_ALPHA);

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CCountdownToRestartDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CRect   rect;
		CPaintDC   dc(this);
		GetClientRect(rect);
		dc.FillSolidRect(rect, RGB(0, 0, 0));   //设置为绿色背景  
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CCountdownToRestartDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



HBRUSH CCountdownToRestartDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) {
	HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);

	// TODO:  在此更改 DC 的任何特性
	switch (pWnd->GetDlgCtrlID()) {
	case IDC_STATIC:
		pDC->SetBkMode(TRANSPARENT);
		pDC->SetTextColor(RGB(30, 144, 255));
		pDC->SelectObject(&m_font); //设置字体格式
		return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
	}

	// TODO:  如果默认的不是所需画笔，则返回另一个画笔
	return hbr;
}


void CCountdownToRestartDlg::OnTimer(UINT_PTR nIDEvent) {
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	if (nIDEvent == REBOOT_TIMER) {
		
		if (m_nClock == 0) {
			KillTimer(REBOOT_TIMER);			
			WinExec("shutdown -r -t 0", SW_HIDE);
			//MessageBox(L"重启");
		}
		else {
			m_nClock--;
			std::wstring wsText = L"首次启动需要重启计算机使相关配置生效，即将为您重启计算机，请耐心等待，倒计时" + std::to_wstring(m_nClock);
			m_cTooltipText->SetWindowTextW(wsText.c_str());
		}
	}
	CDialogEx::OnTimer(nIDEvent);
}